package com.sale.system.service.impl;

import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sale.common.core.utils.StringUtils;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.common.satoken.utils.LoginHelper;
import com.sale.system.api.domain.SysBalance;
import com.sale.system.api.domain.dto.SysMaterialRawResultDto;
import com.sale.system.domain.PullDown;
import com.sale.system.domain.SysAccountManagement;
import com.sale.system.export.SysAccountManagementExport;
import com.sale.system.mapper.MaterialFinishedMapper;
import com.sale.system.mapper.SysAccountManagementMapper;
import com.sale.system.service.ISysAccountManagementService;
import com.sale.system.service.ISysDocumentService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author: YSL
 * @Date: 2023/7/5 15:36
 * @Desc: 账户管理 服务层实现类
 */
@RequiredArgsConstructor
@Service
public class SysAccountManagementServiceImpl implements ISysAccountManagementService {

    /**
     * 账户管理数据层
     */
    private final SysAccountManagementMapper sysAccountManagementMapper;

    /**
     * 编码规则
     */
    private final ISysDocumentService sysDocumentService;

    private final MaterialFinishedMapper sysMaterialRawMapper;

    /**
     * 分页查询账户管理列表
     *
     * @param sysAccountManagement
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<SysAccountManagement> selectAccountManagementList(SysAccountManagement sysAccountManagement, PageQuery pageQuery) {
        // 模糊查询
        LambdaQueryWrapper<SysAccountManagement> lambdaQueryWrapper = new LambdaQueryWrapper<SysAccountManagement>()
            .like(StringUtils.isNotBlank(sysAccountManagement.getAccountName()), SysAccountManagement::getAccountName, sysAccountManagement.getAccountName())
            .like(StringUtils.isNotBlank(sysAccountManagement.getAccountCode()), SysAccountManagement::getAccountCode, sysAccountManagement.getAccountCode());
        // 分页
        Page<SysAccountManagement> sysAccountManagementPage = sysAccountManagementMapper.selectPage(pageQuery.build(), lambdaQueryWrapper);




        return TableDataInfo.build(sysAccountManagementPage);
    }

    /**
     * 分页查询账户管理列表(不分页状态为启用)
     *
     * @param sysAccountManagement
     * @param
     * @return
     */
    @Override
    public List<SysAccountManagement> listNotPage(SysAccountManagement sysAccountManagement) {
        // 模糊查询
        LambdaQueryWrapper<SysAccountManagement> lambdaQueryWrapper = new LambdaQueryWrapper<SysAccountManagement>()
            .eq(SysAccountManagement::getEnabledState,1);
        // 不分页
        List<SysAccountManagement> sysAccountManagementList = sysAccountManagementMapper.selectList(lambdaQueryWrapper);
        return sysAccountManagementList;
    }

    /**
     * 根据id查询账户管理详情
     *
     * @param accountId
     * @return
     */
    @Override
    public SysAccountManagement getAccountManagementById(Long accountId) {
        return sysAccountManagementMapper.selectById(accountId);
    }

    /**
     * 新增账户管理
     *
     * @param sysAccountManagement
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertAccountManagement(SysAccountManagement sysAccountManagement) {
        sysAccountManagement.setCreateBy(LoginHelper.getUsername());
        sysAccountManagement.setCreateTime(new Date());
        sysAccountManagement.setEnabledState(1);
        sysAccountManagement.setCurrentBalance(sysAccountManagement.getInitialBalance());
        // 依据编码规则生成账户编码
        sysAccountManagement.setAccountCode(sysDocumentService.selectAuthorityCoding("system:account:list"));
        return sysAccountManagementMapper.insert(sysAccountManagement);
    }

    /**
     * 修改账户管理
     *
     * @param sysAccountManagement
     * @return
     */
    @Override
    public int updateAccountManagement(SysAccountManagement sysAccountManagement) {
        sysAccountManagement.setUpdateBy(LoginHelper.getUsername());
        sysAccountManagement.setUpdateTime(new Date());
        return sysAccountManagementMapper.updateById(sysAccountManagement);
    }

    /**
     * 删除账户管理信息
     * @param accountId
     * @return
     */
    @Override
    public int deleteAccountManagement(Long accountId) {
        return sysAccountManagementMapper.deleteById(accountId);
    }

    /**
     * 导出账户管理集合
     * @return
     */
    @Override
    public List<SysAccountManagementExport> selectAccountDeriveList(SysAccountManagement sysAccountManagement) {
        return sysAccountManagementMapper.selectAccountManagementExportList(sysAccountManagement);
    }

    /**
     * 修改账户当前余额
     */
    @Override
    public int editBalance(SysBalance sys) {
        return sysMaterialRawMapper.editBalance(sys);
    }

    /**
     * 循环账户当前余额
     */
    @Override
    public int editListBalance(List<SysBalance> list1) {
        int i =0;
        for (SysBalance sys:
        list1) {
            i=sysMaterialRawMapper.editBalance(sys);
        }
        return i;
    }

    /**
     * 循环账户当前余额 -
     */
    @Override
    public int editListBalance2(List<SysBalance> list1) {
        int i =0;
        for (SysBalance sys:
            list1) {
            i=sysMaterialRawMapper.editBalance2(sys);
        }
        return i;
    }

    /**
     * 修改账户当前余额 -
     */
    @Override
    public int editBalance2(SysBalance sys) {
        return sysMaterialRawMapper.editBalance2(sys);
    }

    /**
     * 账户管理 下拉
     */
    @Override
    public List<PullDown> getManagementDown() {
        return sysAccountManagementMapper.getManagementDown();
    }

}
